package com.egao.common.manage.school;

import cn.hutool.core.util.StrUtil;
import com.egao.common.core.annotation.ApiPageParam;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.student.entity.StudentPoverty;
import com.egao.common.module.student.service.StudentPovertyService;
import com.egao.common.module.system.service.CollegeService;
import com.egao.common.module.system.service.MajorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 学校管理学生贫困信息
 * 涵精准帮扶和52县
 * @author lym
 */
@Api(tags = "学校管理学生贫困信息")
@RestController
@RequestMapping("${api}")
public class SchoolPovertyController extends BaseController {

    @Autowired
    private StudentPovertyService studentPovertyService;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private MajorService majorService;

    private final String title = "学校学生贫困管理";

    @PreAuthorize("hasAuthority('school:poverty:list')")
    @OperLog(value = title, desc = "分页查询")
    @ApiOperation(value = "分页查询")
    @ApiPageParam
    @GetMapping("/school/poverty/page")
    public PageResult<StudentPoverty> page(HttpServletRequest request) {
        PageParam<StudentPoverty> pageParam = new PageParam<>(request);
        // 异常情况 没有找到类型必须条件 返回空数据
        if (StrUtil.isBlank(pageParam.getString("category"))) {
            return new PageResult<>();
        }
        return new PageResult<>(studentPovertyService.listPage(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('school:poverty:list')")
    @ApiOperation(value = "学院集合")
    @GetMapping("/school/poverty/college")
    public JsonResult college() {
        return JsonResult.ok().setData(collegeService.listAll( null, ""));
    }

    @PreAuthorize("hasAuthority('school:poverty:list')")
    @ApiOperation(value = "专业集合")
    @GetMapping("/school/poverty/majors")
    public JsonResult majors() {
        return JsonResult.ok().setData(majorService.listAll("", null));
    }

    @PreAuthorize("hasAuthority('school:poverty:detail')")
    @OperLog(value = title, desc = "查看详情")
    @ApiOperation(value = "查看详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", required = true, dataType = "Integer")
    })
    @GetMapping("/school/poverty/detail")
    public JsonResult detail(Integer id) {
        return studentPovertyService.detail(id);
    }

    @PreAuthorize("hasAuthority('school:poverty:update')")
    @OperLog(value = title, desc = "保存信息", result = true)
    @ApiOperation(value = "保存信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentPoverty", value = "studentPoverty", required = true, dataType = "StudentPoverty")
    })
//    @PutMapping("/school/poverty/submit")
    @PutMapping("/school/poverty/save")
    public JsonResult pickDispatchHiredWay(@RequestBody StudentPoverty studentPoverty) {
        return studentPovertyService.updateStudentPoverty(studentPoverty, getLoginUserId());
    }

    @PreAuthorize("hasAuthority('school:poverty:update')")
    @OperLog(value = title, desc = "提交信息", result = true)
    @ApiOperation(value = "提交信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentPoverty", value = "studentPoverty", required = true, dataType = "StudentPoverty")
    })
    @PutMapping("/school/poverty/submit")
    public JsonResult submitStudentPoverty(@RequestBody StudentPoverty studentPoverty) {
        return studentPovertyService.submitStudentPoverty(studentPoverty, getLoginUserId());
    }

    @PreAuthorize("hasAuthority('school:poverty:import')")
    @OperLog(value = title, desc = "导入", result = true)
    @ApiOperation("导入")
    @Transactional
    @PostMapping("/school/poverty/import")
    public JsonResult importBatch(MultipartFile file, String category) {
        return studentPovertyService.importExcel(file, category, getLoginUserId());
    }

    @PreAuthorize("hasAuthority('school:poverty:export')")
    @OperLog(value = title, desc = "导出")
    @ApiOperation(value = "导出")
    @GetMapping("/school/poverty/export")
    public void export(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> pageParam = new PageParam<>(request).getNoPageParam();
        studentPovertyService.exportExcel(pageParam, response);
    }
}
